import router from '@/router'
interface UserStoreState {
  userInfo?: {
    [name: string]: any
  }
  token?: string
}

export const useUserStore = defineStore({
  id: 'user',
  state: (): UserStoreState => ({
    userInfo: {},
    token: '',
  }),
  actions: {
    setUserInfo(userInfo: { [name: string]: any }) {
      this.userInfo = userInfo
    },
    setToken(token: string) {
      this.token = token
    },
    removeUserInfo() {
      router.push('/login')
      this.userInfo = {}
      this.token = ''
    },
  },
  // 持久化插件
  persist: {
    enabled: true,
    strategies: [
      {
        key: 'userInfo',
        storage: localStorage,
      },
    ],
  },
})
